package android.hermes;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * @author Erik, Tobias & Joakim.
 * Class that encodes and decodes messages with files from DataStreams ant to byte[]
 */
public class MsgHandler {
	
	//Instants variables
	private int receiver, sender, hops;
	private String message, fileName;
	private long time;
	private byte[] data, buf;
	
	/**
	 * @param receiver
	 * @param sender
	 * @param hops
	 * @param time
	 * @param fileName
	 * @param message
	 * @return true, if enable to encodes all args to byte[], false otherwise
	 * 
	 */
	public boolean encodeMsg(int receiver, int sender, int hops, long time, String fileName, String message){
		String tempArgs;
		
		try{
		//Writes arguments; 1=receiver(ID), 2=sender(ID), 3=Hops left(int), 4=time left(long), 5=filename(string), 6=message(string)
		tempArgs = receiver + "\n"
				+ sender + "\n"
				+ hops + "\n"
				+ time + "\n"
				+ fileName + "\n"
				+ message;
		
		File file = new File("/data/data/android.hermes/send/"+fileName);
		
		
		//adds null to separate args from data
		byte args[] = (tempArgs+"\u0000").getBytes();
		byte data[] = new byte[(int)file.length()];
		
		FileInputStream FIS = new FileInputStream(file);
		FIS.read(data);
		
		buf = new byte[args.length + (int)file.length()];
		System.arraycopy(args, 0, buf, 0, args.length);
		System.arraycopy(data, 0, buf, args.length, data.length);
		
		} catch (IOException e) {
			// Error handling
			return false;
		}
		return true;
	}
	
	/**
	 * @param indata
	 * @return true if enable to decode indata, otherwise false
	 * 
	 */
	public boolean decodeMsg(DataInputStream indata){
		//initiates to prevent errors
		receiver = 0;
		sender = 0;
		hops = 0;
		time = 0;
		fileName = null;
		message = "";
		data = null;
		
		
		String inString;
		byte buf[], name[] = null;
		int j = 0;
		
		try {
			buf = new byte[indata.available()];
			indata.read(buf);
			//Separates args from data
			for(int i=0; i<buf.length; i++){
				if (j == 0 && buf[i]== 0){
					j = 1;
					name = new byte[i];
					System.arraycopy(buf, 0, name, 0, i);
				}
				else if(j == 1){
					j = 2;
					data = new byte[buf.length-(i)];
					System.arraycopy(buf, i, data, 0, buf.length-i);
					break;
				}
			}
			inString = new String(name);
			Scanner sc = new Scanner(inString);
			//Reads arguments; 1=receiver(ID), 2=sender(ID), 3=Hops left(int), 4=time left(int), 5=filename(string), 6=message(string)
			for(int i=0; sc.hasNextLine(); i++){
				if(i==0){
					receiver = Integer.parseInt(sc.nextLine());
				}
				else if(i==1){
					sender = Integer.parseInt(sc.nextLine());
				}
				else if(i==2){
					hops = Integer.parseInt(sc.nextLine());
				}
				else if(i==3){
					time = Long.parseLong(sc.nextLine());
				}
				else if(i==4){
					fileName = sc.nextLine();
				}
				else{
					message += sc.nextLine()+"\n";
				}
			}
		} catch (IOException e) {
			// Error handling
			return false;
		}
		return true;
	}
	
	
	//Get methods
	public int getReceiver(){
		return receiver;
	}
	public int getSender(){
		return sender;
	}
	public int getHop(){
		return hops;
	}
	public long getTime(){
		return time;
	}
	public String getFileName(){
		return fileName;
	}
	public String getMsg(){
		return message;
	}
	public byte[] getData(){
		return data;
	}
	public byte[] getBuf(){
		return buf;
	}

}
